package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.dzzf.base.kfzx.ClCardRefInfo;
import net.wantongtech.xa.uploadbasicinfo.vo.RefundExcelModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 销卡退费DAO实现
 *
 * @author MaShibo
 * 日期 :  2020/1/3
 */
@Repository
public class CancelCardRefundDao extends BaseDataTemplate {


    public void batchInsertRefundInfo(List<RefundExcelModel> fundModelList) {
        final String sql = " insert into tmp_backmny(vehPlate,cardNo,backMny,sysname,receivename,bankNo,bankname, id, rownumber, fileName, phoneNo)" +
                " values (?,?,?,?,?,?,?, ?, ?, ?, ?) ";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                RefundExcelModel refundModel = fundModelList.get(i);
                ps.setString(1, refundModel.getVehPlate());
                ps.setString(2, refundModel.getCardNo());
                ps.setBigDecimal(3, refundModel.getBackMny());
                ps.setString(4, refundModel.getSysName());
                ps.setString(5, refundModel.getReceiveName());
                ps.setString(6, refundModel.getBankNo());
                ps.setString(7, refundModel.getBankName());
                ps.setString(8, refundModel.getId());
                ps.setObject(9, refundModel.getRowNumber());
                ps.setString(10, refundModel.getFileName());
                ps.setString(11, refundModel.getPhoneNo());
            }

            @Override
            public int getBatchSize() {
                return fundModelList.size();
            }
        });
    }


    public List<RefundExcelModel> queryRefundDataList() {
        final String sql = "select * from tmp_backmny";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(RefundExcelModel.class));
    }

    public List<ClCardRefInfo> queryRefundInfo(String vehPlate, String cardNo) {
        final String sql =
                "SELECT *\n" +
                        "  FROM ac_clcard_refinfo o\n" +
                        " WHERE o.vehicleplate = ?\n" +
                        "   AND (o.cardno = ? OR o.cardno = ?)";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(ClCardRefInfo.class),
                vehPlate, cardNo, StringUtils.substring(cardNo, 4));
    }

    public int updateBankOrderData(String bankOrderNo, String tradeNo) {
        final String sql = " update ac_clcard_refinfo o set o.state = 2,o.bankorderno = ? where o.tradeno = ? ";
        return jdbcTemplate.update(sql, bankOrderNo, tradeNo);
    }

    /**
     * 查询账户信息
     */
    public List<String> queryAccountInfo(String tradeNo) {
        final String sql = " select t.accountid from ac_account t where t.accountid = ( " +
                "select t.accountid from ac_cardinfoopt t where t.tradeno = ? ) ";
        return jdbcTemplate.queryForList(sql, String.class, tradeNo);
    }

    /**
     * 更新账户中的余额信息
     *
     * @param accountId 账户金额
     * @param updateMby 更新的金额
     */
    public int updateAccount(String accountId, BigDecimal updateMby) {
        final String sql = " update ac_account t set t.accountbalance = t.accountbalance - ? where t.accountid = ? ";
        return jdbcTemplate.update(sql, updateMby, accountId);
    }

}
